package com.devplatform.service.impl;

import com.devplatform.mapper.AdStatisticsMapper;
import com.devplatform.pojo.dto.AdDetailStatsDto;
import com.devplatform.pojo.dto.AdStatisticsDto;
import com.devplatform.pojo.dto.ChartDataDto;
import com.devplatform.service.AdStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdStatisticsServiceImpl implements AdStatisticsService {

    @Autowired
    private AdStatisticsMapper adStatisticsMapper;

    @Override
    public AdStatisticsDto getAdStatistics(Date startDate, Date endDate, String position) {
        AdStatisticsDto statistics = new AdStatisticsDto();

        // 获取基础统计数据
        Long totalImpressions = adStatisticsMapper.selectTotalImpressions(startDate, endDate, position);
        Long totalClicks = adStatisticsMapper.selectTotalClicks(startDate, endDate, position);
        Integer activeAdsCount = adStatisticsMapper.selectActiveAdsCount(startDate, endDate, position);

        // 计算平均点击率
        double avgCtr = totalImpressions > 0 ? (totalClicks * 100.0 / totalImpressions) : 0;

        // 获取图表数据
        List<ChartDataDto> chartDataList = adStatisticsMapper.selectChartData(startDate, endDate, position);
        ChartDataDto chartData = processChartData(chartDataList);

        // 获取广告详情统计
        List<AdDetailStatsDto> adDetails = adStatisticsMapper.selectAdDetailStats(startDate, endDate, position);

        // 设置统计数据
        statistics.setTotalImpressions(totalImpressions);
        statistics.setTotalClicks(totalClicks);
        statistics.setAvgCtr(Math.round(avgCtr * 100.0) / 100.0); // 保留两位小数
        statistics.setActiveAdsCount(activeAdsCount);
        statistics.setChartData(chartData);
        statistics.setAdDetails(adDetails);

        return statistics;
    }

    /**
     * 处理图表数据，转换为前端需要的格式
     */
    private ChartDataDto processChartData(List<ChartDataDto> chartDataList) {
        ChartDataDto result = new ChartDataDto();

        if (chartDataList != null && !chartDataList.isEmpty()) {
            // 提取日期标签
            List<String> dateLabels = chartDataList.stream()
                    .map(ChartDataDto::getDateLabel)
                    .collect(Collectors.toList());


            // 提取曝光量数据
            List<Long> impressionData = chartDataList.stream()
                    .map(ChartDataDto::getImpressions)
                    .collect(Collectors.toList());

            // 提取点击量数据
            List<Long> clickData = chartDataList.stream()
                    .map(ChartDataDto::getClicks)
                    .collect(Collectors.toList());

            // 提取点击率数据
            List<Double> ctrData = chartDataList.stream()
                    .map(ChartDataDto::getCtr)
                    .collect(Collectors.toList());

            result.setDateLabels(dateLabels);
            result.setImpressionData(impressionData);
            result.setClickData(clickData);
            result.setCtrData(ctrData);
        }

        return result;
    }

}
